Skip to content

Conversation

@dominikbielinski
Copy link

A test that shows that combining @JoinFormula with @onetomany fails during startup.

@dominikbielinski dominikbielinski changed the base branch from master to 5.3 September 28, 2018 11:06
private String hierarchyPath;

@OneToMany(fetch = FetchType.LAZY)
@JoinFormula(value = "hierarchy_path like id || '/_%'", referencedColumnName = "hierarchy_path")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you annotate the inverse relation? I.e. private Invoice parentInvoice? Not sure whether even JoinColumn is allowed here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that as well:

@ManyToOne(fetch = FetchType.LAZY)
@JoinFormula(value = "SUBSTR(hierarchy_path, 0, INSTR(hierarchy_path, '/') - 1)", referencedColumnName = "id")
private Invoice rootInvoice;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "rootInvoice")
private Set<Invoice> childInvoices;

but there is same exception as described here https://discourse.hibernate.org/t/how-to-use-the-hibernate-joinformula-annotation-with-a-onetomany-jpa-association/1461 (i.e. ManyToOne works with JoinFormula, but OneToMany doesn't - even when "mappedBy" is added and JoinFormula is on ManyToOne side).
I guess the question is just whether JoinFormula works with OneToMany - but if it doesn't, how is anyone supposed to know that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an existing test with a @JoinFormula @ManyToOne relation here: https://github.com/hibernate/hibernate-orm/blob/e5dc635a52362f69b69acb8d5b166b69b165dbbd/hibernate-core/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java . So what you're saying is that when this example is extended with an inverse mapping (a @OneToMany(mappedBy = ""), Hibernate fails to boot?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's exactly what I was trying to say. For instance, in the test that you mentioned, if you add following to Language entity:

private List<Message> messages;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "language")
public List<Message> getMessages() {
    return messages;
}

public void setMessages(List<Message> messages) {
    this.messages = messages;
}

the test fails with exception
Caused by: java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column

@gsmet
Copy link
Member

gsmet commented Nov 2, 2018

@vladmihalcea I would be interested in having your feedback on that.

While I'm not sure this particular test case is correct, @jwgmeligmeyling has a point here: #2547 (comment) e.g. that a test does not work when we just add an inverse relationship.

I'm wondering if it's even possible to support an inverse relationship with @JoinFormula.

WDYT?

@vladmihalcea
Copy link
Contributor

@gsmet @jwgmeligmeyling I think it's a valid use case and it would be convenient to add support for it.

@gavinking
Copy link
Member

I'm not sure we will ever implement this feature, but we should have a meaningful error, hence #9320.

@gavinking gavinking closed this Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants